import {
  getFileTypeCount,
  getIndustryCount,
  getLevelCount,
  getOriginList,
} from '@/services/ant-design-pro/foundation';
import { ActionType } from '@ant-design/pro-components';
import { useLocation } from '@umijs/max';
import { useRequest } from 'ahooks';
import { useEffect, useRef, useState } from 'react';
import { createContainer } from 'unstated-next';
export default createContainer(() => {
  const [activeIndex, setActiveIndex] = useState<string | number>('');
  const [activeKey, setActiveKey] = useState<string>('11');
  const [name, setName] = useState<string>('');

  const [data, setData] = useState<any[]>([]);
  const [levelData, setLevelData] = useState<any[]>([]);
  const actionRef = useRef<ActionType>();
  const [url, setUrl] = useState<string>('');
  const [type, setType] = useState<string>('');
  const [jsonViewVisible, setJsonViewVisible] = useState<boolean>(false);

  const [viewVisible, setViewVisible] = useState<boolean>(false);

  const location: Record<string, any> = useLocation();
  const [typeData, setTypeData] = useState<{ name: string; countNumber: number }[]>([]);

  const [selectedKeys, setSelectedKeys] = useState<any[]>([]);

  const id = location?.state?.knowledgeId ? location?.state?.knowledgeId : '';

  const getOriginListData = useRequest(
    async (val) => {
      const res = await getOriginList({
        ...val,

        knowledgeId: id,
      });
    },
    { manual: true },
  );
  const getFileTypeCountData = useRequest(
    async () => {
      const res = await getFileTypeCount(id);
      if (res.data) {
        setData(res.data);
      }
    },

    { manual: true },
  );
  const getLevelCountData = useRequest(
    async () => {
      const res = await getLevelCount(id);
      if (res.data) {
        setLevelData(res.data);
      }
    },

    { manual: true },
  );

  const getData = useRequest(
    async () => {
      const res = await getIndustryCount(id);
      setTypeData(res.data);
    },
    { manual: true },
  );

  useEffect(() => {
    getFileTypeCountData.run();
    getLevelCountData.run();
    getData.run();
  }, [id]);
  return {
    activeIndex,
    setActiveIndex,
    activeKey,
    setActiveKey,
    getOriginListData,
    name,
    setName,
    actionRef,
    getFileTypeCountData,
    data,
    id,
    typeData,
    viewVisible,
    setViewVisible,
    url,
    setUrl,
    setType,
    type,
    jsonViewVisible,
    setJsonViewVisible,

    selectedKeys,
    setSelectedKeys,
    levelData,
  };
});
